var setting = {
    username: '',
    password: '',
    userPower: [],
    tabs: [],
    selectedTab: ''
}
var V;
var vue = new Vue({
    el: '.outer',
    data: setting,
    methods: {
        select: function (index) {
            var flag = true;
            vue.tabs.forEach(function (menu) {
                if(menu.menu_id.toString() == index){
                    vue.selectedTab = menu.menu_id.toString();
                    flag = false;
                }
            });
            if(flag){
                $.post("menu/getMenuById", {menu_id: index}, function (menu) {
                    vue.tabs.push(menu);
                    vue.selectedTab = menu.menu_id.toString();
                })
            }
        },
        removeTab: function (tagName) {
            var nextTagName = '';
            var tabs = vue.tabs;
            if (vue.selectedTab == tagName) {
                tabs.forEach(function (menu, index) {
                    if(menu.menu_id.toString() == tagName){
                        var nextTag = tabs[index + 1] || tabs[index - 1];
                        if(nextTag){
                            nextTagName = nextTag.menu_id.toString();
                        }
                    }
                });
            }

            if(nextTagName != ''){
                vue.selectedTab = nextTagName;
            }
            vue.tabs = tabs.filter(function (menu) {
                return tagName != menu.menu_id.toString();
            })
        },
        lockScreen: function () {
            /*vue.$alert('暂未实现', '提示', {
                confirmButtonText: '确定',
            });*/
            //vue.$message.warning('暂未实现');
            lock();
        },
        openChangePaswPage: function () {
            var menu = {
                menu_id: 'changePassword',
                title: '修改密码',
                icon: 'fa fa-key',
                href: '/changePwd.html'
            }
            vue.tabs.push(menu);
            vue.selectedTab = menu.menu_id;
        },
        logout: function () {
            $.post('/authority/logout',function () {
                location.reload()
            })
        }
    }
})
$.post("authority/getLoginUser", function (user) {
    vue.username = user.user_name;
})
$.post("authority/getLoginUserPower", function (powers) {
    vue.userPower = powers;
})
$.post("authority/getLockScreenState", function (flag) {
    if(flag){
        lock()
    }
})
//锁屏
function lock() {
    $.post('/authority/lockScreen', function () {
        var h = vue.$createElement;
        vue.$msgbox({
            message: h('p', null, [
                h('span', null, '请输入密码： '),
                h('el-input', {attrs: {type: 'password'},props:{password: ''},domProps:{value: vue.password}}, '请输入密码： '),
            ]),
            confirmButtonText: '确定',
            showCancelButton: false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
            showInput: false,
            inputPlaceholder: '请输入登陆密码',
            inputType: 'password',
            inputValidator: function (value) {
                if(value == ''){
                    return '密码不能为空';
                }else{
                    return true;
                }
            },
            beforeClose: function (action, instance, done) {
                if(action == 'cancel'){
                    return
                }else {
                    //console.info(instance.$children[2].currentValue)
                    $.post('/authority/unlockScreen', {password: instance.$children[2].currentValue}, function (msg) {
                        if(msg.status == 'SUCCESS'){
                            done();
                        }else{
                            vue.$message.error(msg.message);
                        }
                    })
                }
            }
        })
    })
}